package com.heima.user.listener;

import com.alibaba.fastjson.JSON;
import com.heima.common.aliyun.util.AliSendSms;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

@Component
public class SendSmsListener {

    @KafkaListener(topics = "sendSms-topic")
    public void receiverMessage(ConsumerRecord<?,?> record){
        if (Optional.ofNullable(record).isPresent()){
            Map<String, String> map = JSON.parseObject((String) record.value(), Map.class);
            String telephone = map.get("telephone");
            String code = map.get("code");
            AliSendSms.sendSms(telephone,code);
        }
    }
}
